package simpleModule.view {
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.Canvas;
	import mx.controls.Image;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.mediator.FlexMediator;	
	
	public class SimpleModuleMediator extends FlexMediator 
	{
		private var images:XML;
		public var pictures:Array = new Array();
		public var index:int;
		private var timer:Timer;
		
		public function SimpleModuleMediator(viewComponent : SimpleModule) 
		{
			super(NAME, viewComponent);
			app.nextButton.addEventListener(MouseEvent.CLICK, reactToNextButtonClick);
			app.previousButton.addEventListener(MouseEvent.CLICK, reactToPreviousButtonClick);
			app.playButton.addEventListener(MouseEvent.CLICK, reactToPlayButtonClick);
			app.pauseButton.addEventListener(MouseEvent.CLICK, reactToPauseButtonClick);
		}
		
		private function reactToPlayButtonClick(event:MouseEvent):void
		{
			app.playButton.enabled=false;
			app.pauseButton.enabled=true;
			timer=new Timer(2000,0);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER,timerListener);	
		}
		
		private function timerListener(e:TimerEvent):void
		{
			 reactToNextButtonClick;
			if(app.myViewStack.selectedIndex == images.image.length())
			{
				app.myViewStack.selectedIndex=0;
			}
			if(app.randomCheckBox.selected == true)
			{
				trace("kichcha --> random selected");
				app.myViewStack.selectedIndex = Math.floor(Math.random()*images.image.length());
			} 	
		}
		
		private function destroy():void
		{
			timer.removeEventListener(TimerEvent.TIMER,timerListener);
		}
		
		private function reactToPauseButtonClick(event:MouseEvent):void
		{
			app.pauseButton.enabled=false;
			destroy();
			app.playButton.enabled=true;
		}
		
		private function reactToPreviousButtonClick(event:MouseEvent):void
		{
			effects();
			if(app.cbData.selectedIndex==4)
			{
				app.rotate2.play([app.myViewStack]);
			}
			if(app.myViewStack.selectedIndex!=1)
			{
				app.nextButton.enabled=true;
			}
			else
			{
				app.previousButton.enabled=false;
			}
			app.myViewStack.selectedIndex--;
		}
		
        public function reactToNextButtonClick(event:MouseEvent = null):void
        {
        	effects();
			if(app.cbData.selectedIndex==4)
			{
				app.rotate1.play([app.myViewStack]);
			}
			if(app.myViewStack.selectedIndex < images.image.length()-2)
			{
				app.previousButton.enabled=true;
			}
			else
			{
				app.nextButton.enabled=false;
			}
			app.myViewStack.selectedIndex++;
        }
        
        private function effects():void
			{
				if(app.cbData.selectedIndex==5)
				{
					app.resize.play([app.myViewStack]);
				}
				if(app.cbData.selectedIndex==0)
				{
					app.fadeOut.play([app.myViewStack]);
				}
				else if(app.cbData.selectedIndex==1)
				{
					app.myWR.play([app.myViewStack]);
				}
				else if(app.cbData.selectedIndex==2)
				{
					app.myWD.play([app.myViewStack]);
				}
				else if(app.cbData.selectedIndex==3)
				{
					app.myWU.play([app.myViewStack]);
				}
			}
		
		override public function onRegister():void
		{
			
		}
		
		public function respondToXmlLoaded(note:INotification):void
		{
			images = new XML(note.getBody());
			for(index=0; index<images.image.length();index++)
			{
				var canvas:Canvas = new Canvas();
				var webImage:Image = new Image();
				webImage.source=images.image.photo[index].toString();
				webImage.addEventListener(Event.COMPLETE,handleImageLoadComplete);
				webImage.addEventListener(IOErrorEvent.IO_ERROR,handleImageLoadError);
				webImage.load();
				canvas.addChild(webImage);
				app.myViewStack.addChild(canvas);
			}
				app.captionText.text = images.image.photocaption[app.myViewStack.selectedIndex].toString();
		}
		
		private function handleImageLoadComplete(e:Event):void
		{	
			trace("target image "+viewComponent.myViewStack.getChildAt(viewComponent.myViewStack.selectedIndex));
			var scale:Number;
			var IW:Number=e.target.content.width;
			var IH:Number=e.target.content.height;
			var PW:Number=viewComponent.myViewStack.width;
			var PH:Number=viewComponent.myViewStack.height;
			var imageWidth:Number;
			var imageHeight:Number;
			if(IW>IH)
			{
				scale=PW/IW;
				imageHeight=IH*scale;
				imageWidth=PW;
			}
			else
			{
				scale=PH/IH;
				imageWidth=IW*scale;
				imageHeight=PH;
			}
			trace("image width "+e.target.parent);
			e.target.content.width=imageWidth;
			e.target.content.height=imageHeight;
			e.target.content.x=(PW-imageWidth)/2;
			e.target.content.y=(PH-imageHeight)/2;  
		}
		
		private function handleImageLoadError(e:Event):void
		{
			trace("Image load Error");
		}
		
		public function get app() : SimpleModule 
		{
			return viewComponent as SimpleModule;
		}
	}
}